package com.zoffy.wmsbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoffy.wmsbackend.common.ErrorCode;
import com.zoffy.wmsbackend.common.RequestParam;
import com.zoffy.wmsbackend.exception.BusinessException;
import com.zoffy.wmsbackend.mapper.MaterialUsagePlanMapper;
import com.zoffy.wmsbackend.mapper.WarehouseMapper;
import com.zoffy.wmsbackend.model.dto.materialUsagePlan.MaterialPlanDetailAddDto;
import com.zoffy.wmsbackend.model.dto.materialUsagePlan.MaterialUsagePlanDto;
import com.zoffy.wmsbackend.model.entity.Goods;
import com.zoffy.wmsbackend.model.entity.MaterialUsagePlan;
import com.zoffy.wmsbackend.model.entity.MaterialUsagePlanDetail;
import com.zoffy.wmsbackend.model.entity.Warehouse;
import com.zoffy.wmsbackend.model.vo.materialUsagePlan.MaterialUsagePlanDetailVo;
import com.zoffy.wmsbackend.model.vo.materialUsagePlan.MaterialUsagePlanDetailsVo;
import com.zoffy.wmsbackend.model.vo.materialUsagePlan.MaterialUsagePlanVo;
import com.zoffy.wmsbackend.service.GoodsService;
import com.zoffy.wmsbackend.service.MaterialUsagePlanService;
import com.zoffy.wmsbackend.model.dto.materialUsagePlan.MaterialPlanAddDto;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MaterialUsagePlanServiceImpl extends ServiceImpl<MaterialUsagePlanMapper, MaterialUsagePlan> implements MaterialUsagePlanService {
    @Resource
    private GoodsService goodsService;
    @Resource
    private WarehouseMapper warehouseMapper;
    @Resource
    private MaterialUsagePlanDetailServiceImpl materialUsagePlanDetailService;
    @Resource
    private MaterialUsagePlanMapper materialUsagePlanMapper;
    @Override
    public Page<MaterialUsagePlanVo> listMaterialUsagePlanVoByPage(RequestParam<MaterialUsagePlanDto> requestParam) {
        Long pageSize = requestParam.getPageSize();
        Integer pageNum = requestParam.getPageNum();
        MaterialUsagePlanDto dto = requestParam.getParams();
        Page<MaterialUsagePlanVo> page = new Page<>(pageNum, pageSize);
        return baseMapper.listMaterialUsagePlanVoByPage(page, dto);
    }
    @Override
    public Boolean addMaterialUsagePlan(MaterialPlanAddDto materialPlanAddDto) {
        String planNo = materialPlanAddDto.getPlanNo();
    String warehouseNo = materialPlanAddDto.getWarehouseNo();
    List<MaterialPlanDetailAddDto> planDetails = materialPlanAddDto.getPlanDetails();

    // 获取商品映射
    Map<String, Goods> goodsMap = goodsService.list().stream().collect(Collectors.toMap(Goods::getGoodsCode, k -> k));

    // 查询仓库信息
    LambdaQueryWrapper<Warehouse> queryWrapper = Wrappers.lambdaQuery();
    queryWrapper.eq(Warehouse::getWarehouseCode, warehouseNo);
    Warehouse warehouse = warehouseMapper.selectOne(queryWrapper);

    // 检查仓库是否存在
    if (warehouse == null) {
        throw new BusinessException(ErrorCode.PARAMS_ERROR, "仓库不存在");
    }

    // 创建用料计划主记录
    MaterialUsagePlan materialUsagePlan = new MaterialUsagePlan();
    materialUsagePlan.setPlanNo(planNo);
    materialUsagePlan.setWarehouseId(warehouse.getId());
    materialUsagePlan.setStatus(0); 
    this.save(materialUsagePlan);

    // 创建用料计划明细记录
    List<MaterialUsagePlanDetail> materialUsagePlanDetails = new ArrayList<>();
    if(planDetails == null || planDetails.isEmpty()){
        planDetails=new ArrayList<>();
    }
    for (MaterialPlanDetailAddDto planDetail : planDetails) {
        MaterialUsagePlanDetail materialUsagePlanDetail = new MaterialUsagePlanDetail();
        materialUsagePlanDetail.setMaterialUsagePlanId(materialUsagePlan.getId());
        materialUsagePlanDetail.setPlanDetailNo(planDetail.getPlanDetailNo());
        materialUsagePlanDetail.setWarehouseId(warehouse.getId());
        materialUsagePlanDetail.setGoodsId(goodsMap.get(planDetail.getGoodsNo()).getId());
        materialUsagePlanDetail.setQuantity(planDetail.getNum());
        materialUsagePlanDetail.setStatus(0); 
        materialUsagePlanDetails.add(materialUsagePlanDetail);
    }

    // 批量保存用料计划明细记录
    materialUsagePlanDetailService.saveBatch(materialUsagePlanDetails);

    return true;
    }
    @Override
    public Boolean approval(Long id) {
        MaterialUsagePlan plan = this.getById(id);
        if (plan != null) {
            if(plan.getStatus() == 0){
                plan.setStatus(45);
            }else{
                plan.setStatus(90);
            }
            return this.updateById(plan);
        }
        return false;
    }

    @Override
    public MaterialUsagePlanDetailsVo getDetailsById(Long id) {
        MaterialUsagePlanVo materialUsagePlanVo = materialUsagePlanMapper.getMaterialUsagePlanVoById(id);
        // 获取用料计划的详细信息
        List<MaterialUsagePlanDetailVo> details = materialUsagePlanMapper.getDetailsById(id);

        MaterialUsagePlanDetailsVo materialUsagePlanDetailsVo = new MaterialUsagePlanDetailsVo();
        materialUsagePlanDetailsVo.setId(materialUsagePlanVo.getId());
        materialUsagePlanDetailsVo.setWarehouseCode(materialUsagePlanVo.getWarehouseCode());
        materialUsagePlanDetailsVo.setWarehouseName(materialUsagePlanVo.getWarehouseName());
        materialUsagePlanDetailsVo.setWarehouseId(materialUsagePlanVo.getWarehouseId());
        materialUsagePlanDetailsVo.setPlanNo(materialUsagePlanVo.getPlanNo());
        materialUsagePlanDetailsVo.setStatus(materialUsagePlanVo.getStatus());
        materialUsagePlanDetailsVo.setDetails(details);

        return materialUsagePlanDetailsVo;
    }
}